/*
 * @Description: 
 * @Author: tanggx
 * @Date: 2022-01-14 10:16:35
 */
//---张瀚---仅在agcloud的tomcat框架下，用于打包放k8s运行时保留，如果是nginx一类则应该视情况移除
//下面的代码是从服务器中获得地址清单，然后更新sdk下的包
agcim.net.apiPath.setAgcimViewerRest("http://19.104.43.207/agcim-viewer-rest");
agcim.net.apiPath.setOpusPortalRest("http://19.104.43.207/agcloud-portal");
agcim.net.apiPath.setOpusFrontSSO("http://19.104.43.207/agcloud-sso");
agcim.net.apiPath.setAgcimServerBIM("http://19.104.43.207/bim-rest");
agcim.net.apiPath.setWpsRest("http://19.104.43.207/wps-rest");
agcim.net.apiPath.setAgcloudMeta("http://19.104.43.207/agcloud-meta");
agcim.net.apiPath.setDynamicRest("http://19.104.43.207/dynamic-rest");

//登录请求所需的clientId配置
agcim.net.apiPath.setLoginAuth("Basic " + btoa("opus-admin:opus-admin123"));
//尝试获取获取配置，仅在agcloudui框架容器中可以调用到
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "/agcim-viewer-ui/getSystemEnv.public", false);
httpRequest.setRequestHeader("Authorization", "bearer " + localStorage.getItem("access_token"));
httpRequest.onreadystatechange = () => {
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        try {
            //更新配置
            let env = JSON.parse(JSON.parse(httpRequest.responseText).message);
            agcim.net.apiPath.setAgcimViewerRest(changeUrl(env.agcimViewerRest, top.location.href));
            agcim.net.apiPath.setOpusPortalRest(changeUrl(env.opusPortalRest, top.location.href));
            agcim.net.apiPath.setOpusFrontSSO(changeUrl(env.opusFrontSSO, top.location.href));
            agcim.net.apiPath.setAgcimServerBIM(changeUrl(env.agcimServerBIM, top.location.href));
            agcim.net.apiPath.setWpsRest(changeUrl(env.wpsRest, top.location.href));
            agcim.net.apiPath.setAgcloudMeta(changeUrl(env.agcloudMeta, top.location.href));
            agcim.net.apiPath.setDynamicRest(changeUrl(env.dynamicRest, top.location.href))
            console.log("更新配置接口信息");
        } catch (error) {
            console.log("无法解析配置文件，继续使用默认配置");
        }
    }

    function changeUrl(oldItem, newItem) {
        if (!oldItem) return oldItem;
        var a = newItem;
        if (getIp(newItem) != null) {
            a = getIp(newItem);
        }
        if (getIp(oldItem) != null) {
            var b = getIp(oldItem);
        } else {
            var b = oldItem;
        }
        var result = oldItem.replace(b, a);
        return result.toString();
    }

    function getIp(str) {
        var pattern = /(\/\/(\S*)\/)/;
        if (!str.match(pattern)) {
            return null;
        }
        var str1 = str.match(pattern)[0].substring(2, str.match(pattern)[0].length - 1);
        var result = str1.substring(0, str1.indexOf("/")) || null;
        return result;
    }
};
httpRequest.send();

//下面的方法和变量只是给agcloud的js里面调用，使其不会报错而已
function vueElePageInit() {
}
